from django.db.models.signals import post_save
from django.contrib.auth.models import User

import multiprofiles.utils as utils

_CustomUserClass = utils.get_user_class()

def create_profile(sender, **kwargs):
    user = kwargs['instance']
    if kwargs['created'] or user.invoke_profile_change:
        ProfileClass = utils.get_profile_class(user.profile_type)
        profile = ProfileClass()
        profile.user = user
        profile.save()
        user.invoke_profile_change = False
        user.save()
post_save.connect(create_profile, _CustomUserClass)